Accelerating 2d object collision with other objects [on hold]

Posted by Silent Cave on Game Development See other posts from Game Development or by Silent Cave
Published on 2013-08-02T10:53:30Z Indexed on 2013/08/02 16:07 UTC
Read the original article Hit count: 307

Filed under:

Making my very first attempt at game programming with SDL/OpenGL. So I made an object Actor witch can move in all four sides with acceleration. And there are bunch of other rectangles to collide to.

the image

Movement and collision detection alghorythms work just fine by itself, but when combined to prevent the green rectangle from crossing black rectangles, it gives me a kind of funny resault. Let me show you the code first:

from Actor.h

class Actor{
public:
    SDL_Rect * dim;
    alphaColor * col;
    float speed;
    float xlGrav, xrGrav, yuGrav, ydGrav;
    float acceleration;
    bool left,right,up,down;
    Actor(SDL_Rect * dim,alphaColor * col, float speed, float acceleration);
    bool colides(const SDL_Rect & rect);
    bool check_for_collisions(const std::vector<SDL_Rect*> & gameObjects );
};

from actor.cpp

bool Actor::colides(const SDL_Rect & rect){
    if     (dim->x + dim->w < rect.x)      return false;
    if     (dim->x > rect.x + rect.w)      return false;
    if     (dim->y + dim->h < rect.y)      return false;
    if     (dim->y > rect.y + rect.h)      return false;
    return true;
}

movement logic from main.cpp

        if (actor->left){
            if(actor->xlGrav < actor->speed){
                actor->xlGrav += actor->speed*actor->acceleration;
            }else actor->xlGrav = actor->speed;
            actor->dim->x -= actor->xlGrav;
            if(actor->check_for_collisions(gameObjects)){
                actor->dim->x += actor->xlGrav;
                actor->xlGrav = 0;
            }
        }
        if (!actor->left){
            if(actor->xlGrav - actor->speed*actor->acceleration > 0){
                actor->xlGrav -= actor->speed*actor->acceleration;
            }else actor->xlGrav = 0;
            actor->dim->x -= actor->xlGrav;
            if(actor->check_for_collisions(gameObjects)){
                actor->dim->x += actor->xlGrav;
                actor->xlGrav = 0;
            }
        }
        if (actor->right){
            if(actor->xrGrav < actor->speed){
                actor->xrGrav += actor->speed*actor->acceleration;
            }else actor->xrGrav = actor->speed;
            actor->dim->x += actor->xrGrav;
            if(actor->check_for_collisions(gameObjects)){
                actor->dim->x -= actor->xrGrav;
                actor->xrGrav = 0;
            }
        }
        if (!actor->right){
            if(actor->xrGrav - actor->speed*actor->acceleration > 0){
                actor->xrGrav -= actor->speed*actor->acceleration;
            }else actor->xrGrav = 0;
            actor->dim->x += actor->xrGrav;
            if(actor->check_for_collisions(gameObjects)){
                actor->dim->x -= actor->xrGrav;
                actor->xrGrav = 0;
            }
        }
        if (actor->up){
            if(actor->yuGrav  < actor->speed){
                actor->yuGrav += actor->speed*actor->acceleration;
            }else actor->yuGrav = actor->speed;
            actor->dim->y -= actor->yuGrav;
            if(actor->check_for_collisions(gameObjects)){
                actor->dim->y += actor->yuGrav;
                actor->yuGrav = 0;
            }
        }
        if (!actor->up){
            if(actor->yuGrav - actor->speed*actor->acceleration > 0){
                actor->yuGrav -= actor->speed*actor->acceleration;
            }else actor->yuGrav = 0;
            actor->dim->y -= actor->yuGrav;
            if(actor->check_for_collisions(gameObjects)){
                actor->dim->y += actor->yuGrav;
                actor->yuGrav = 0;
            }
        }
        if (actor->down){
            if(actor->ydGrav < actor->speed){
                actor->ydGrav += actor->speed*actor->acceleration;
            }else actor->ydGrav = actor->speed;
            actor->dim->y += actor->ydGrav;
            if(actor->check_for_collisions(gameObjects)){
                actor->dim->y -= actor->ydGrav;
                actor->ydGrav = 0;
            }
        }
        if (!actor->down){
            if(actor->ydGrav - actor->speed*actor->acceleration > 0){
                actor->ydGrav -= actor->speed*actor->acceleration;
            }else actor->ydGrav = 0;
            actor->dim->y += actor->ydGrav;
            if(actor->check_for_collisions(gameObjects)){
                actor->dim->y -= actor->ydGrav;
                actor->ydGrav = 0;
            }
        }

So, if the green box approaches an obstacle from up or left, everything goes as planned - object stops, and it's acceleration drops to zero. But if it comes from bottom or right, it enters into obstacles inner space and starts strangely dance, I'd rather say move in inverted controls. What do I fail to see?

© Game Development or respective owner

Related posts about collision-detection